//
//  LXPlistManager.m
//  bgms_p
//
//  Created by yafeng.zhu@lachesis-mh.com on 16/3/2.
//  Copyright © 2016年 yafeng.zhu@lachesis-mh.com. All rights reserved.
//

#import "LXPlistManager.h"

@implementation LXPlistManager

//单例
+(LXPlistManager *)manager
{
    static LXPlistManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[LXPlistManager alloc]init];
    });
    
    return manager;
}

/**
 *  获取食物
 *
 *  @return 数组
 */
-(NSArray *)getFoodList
{
    NSString *path = [self getPlistPathWithFileName:@"foodList"];
    NSArray *foodList = [NSArray arrayWithContentsOfFile:path];
    
    return foodList;
}

-(void)writeFoodList:(NSMutableArray *)array
{
    NSString *path = [self getPlistPathWithFileName:@"foodList"];
    [array writeToFile:path atomically:YES];
}

- (NSArray *)readFoodDataCache
{
    NSArray *array =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = array[0];
    NSString *filePathName = [cachePath stringByAppendingPathComponent:@"foodList.plist"];
    NSArray *dataArray = [NSArray arrayWithContentsOfFile:filePathName];
    return dataArray;
}

- (void)writeFoodDataCache:(NSArray *)dataArray
{
    NSArray *array =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = array[0];
    NSString *filePathName = [cachePath stringByAppendingPathComponent:@"foodList.plist"];
    [dataArray writeToFile:filePathName atomically:YES];
}

/**
 *  获取plist文件
 *
 *  @param fileName 名字
 *
 *  @return 路径
 */
-(NSString *)getPlistPathWithFileName:(NSString *)fileName
{
    NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"plist"];
    return path;
}

@end
